package OSMS.action.Boss;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.BossDao;
import OSMS.dao.CustomerDao;
import OSMS.entity.Boss;
import OSMS.entity.Customers;

public class Boss_Update_Pwd_Servlet extends HttpServlet {

	BossDao bdao = new BossDao();
	CustomerDao cdao = new CustomerDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		Object obj_boss = session.getAttribute("boss");
		Boss boss= (Boss)obj_boss;
		int bid = boss.getBid();
		
		String pw_old = request.getParameter("pw_old");
		String pw_new = request.getParameter("pw_new");
		String pw_again = request.getParameter("pw_again");
		System.out.println(pw_old);
		System.out.println(pw_new);
		System.out.println(pw_again);
		if(!pw_old.equals(boss.getBpwd()))
		{
			request.setAttribute("fail_code", 1);
			request.getRequestDispatcher("../boss/fail_to_update_pwd.jsp").forward(request, response);
			return ;
		}
		else if(!pw_new.equals(pw_again))
		{
			request.setAttribute("fail_code", 2);
			request.getRequestDispatcher("../boss/fail_to_update_pwd.jsp").forward(request, response);
			return ;
		}
		else
		{
			boolean flag_udt_pwd = false;
			try {
				flag_udt_pwd = bdao.upDateBossPwd(pw_new,bid);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Boss after_boss = new Boss();
			try {
				after_boss = bdao.getBossById(boss.getBid());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			session.setAttribute("boss", after_boss);
			
			request.getRequestDispatcher("../boss/successful_to_update_pwd.jsp").forward(request, response);
			
		}
				
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
}
